package com.ruoyi.project.payment.payrate.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MoneyUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.payment.order.domain.Order;
import com.ruoyi.project.payment.order.service.IOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/payment/payrate")
public class PayrateController extends BaseController {

    private String prefix = "payment/payrate";

    @Autowired
    IOrderService orderService;

    @RequiresPermissions("payment:payrate:view")
    @GetMapping("/{app}")
    public String payrate(ModelMap mmap, @PathVariable("app") String app) {

        List<String> dates = new ArrayList<>();
        List<Integer> allDatas = new ArrayList<>();
        List<Integer> payDatas = new ArrayList<>();
        List<Float> payrateDatas = new ArrayList<>();

        Date now = DateUtils.getNowDate();

        int maxPay = 0;

        for (int i = 15; i >= 0; i--) {

            String date = DateUtils.dateTime(DateUtils.addDays(now, i * -1));

            int total = orderService.getOrderCount(app, date, -1);
            int pay = orderService.getOrderCount(app, date, 1);

            dates.add(date);
            allDatas.add(total);
            payDatas.add(pay);

            if (maxPay == 0 || maxPay < pay) {
                maxPay = pay;
            }
            float payrate = 0;
            if (total > 0) {
                payrate = (float) (pay * 100.0) / total;
            }
            if (payrate > maxPay) {
                maxPay = (int) payrate;
            }

            payrate = new BigDecimal(payrate).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            payrateDatas.add(payrate);
        }
        mmap.put("dates", dates);
        mmap.put("appName", StringUtils.equalsAnyIgnoreCase(app, "wyw") ? "文言文" : "语文100分");
        mmap.put("allDatas", allDatas);
        mmap.put("payDatas", payDatas);
        mmap.put("payrateDatas", payrateDatas);
        mmap.put("maxPay", (maxPay / 10 + 1) * 10);

        return prefix + "/payrate";
    }

    @RequiresPermissions("payment:payrate:view")
    @GetMapping("/paystat")
    public String paystat(ModelMap mmap, Integer days) {

        if (days == null) {
            days = 7;
        }
        mmap.put("days", days);

        List<String> dates = new ArrayList<>();
        List<Integer> allDatas = new ArrayList<>();
        List<Integer> dictDatas = new ArrayList<>();
        List<Integer> dictiosDatas = new ArrayList<>();
        List<Integer> poemDatas = new ArrayList<>();
        List<Integer> wywDatas = new ArrayList<>();
        List<Integer> yuwenWeixinDatas = new ArrayList<>();
        List<Integer> dictkxDatas = new ArrayList<>();
        List<Integer> dictswDatas = new ArrayList<>();
        List<Integer> dictchengyuDatas = new ArrayList<>();
        List<Integer> dicianhyDatas = new ArrayList<>();
        List<Integer> gs300Datas = new ArrayList<>();
        List<Integer> dictkxiosDatas = new ArrayList<>();
        List<Integer> dictswiosDatas = new ArrayList<>();
        List<Integer> poemiosDatas = new ArrayList<>();

        Date now = DateUtils.getNowDate();

        for (int i = days; i >= 0; i--) {

            String date = DateUtils.dateTime(DateUtils.addDays(now, i * -1));

            int wyw = orderService.getOrderAmount("wyw", "", date, 1);
            int dict = orderService.getOrderAmount("kkdict", "", date, 1);
            int kkdictios = orderService.getOrderAmount("kkdictios", "", date, 1);
            int poem = orderService.getOrderAmount("kkpoem", "", "apple", date, 1);
            int yuwenWeixin = orderService.getOrderAmount("kkyuwen100", "", date, 1);
            int dictkx = orderService.getOrderAmount("dictkx", "", date, 1);
            int dictsw = orderService.getOrderAmount("dictsw", "", date, 1);
            int dictchengyu = orderService.getOrderAmount("dict_chengyu", "", date, 1);
            int cidianhy = orderService.getOrderAmount("cidianhy", "", date, 1);
            int gs300 = orderService.getOrderAmount("gs300", "", date, 1);
            int dictkxios = orderService.getOrderAmount("dictkxios", "", date, 1);
            int dictswios = orderService.getOrderAmount("dictswios", "", date, 1);
            int poemios = orderService.getOrderAmount("kkpoem", "apple", "", date, 1);

            wyw = MoneyUtil.fee2yuan(wyw).intValue();
            dict = MoneyUtil.fee2yuan(dict).intValue();
            kkdictios = MoneyUtil.fee2yuan(kkdictios).intValue();
            poem = MoneyUtil.fee2yuan(poem).intValue();
            yuwenWeixin = MoneyUtil.fee2yuan(yuwenWeixin).intValue();
            dictkx = MoneyUtil.fee2yuan(dictkx).intValue();
            dictsw = MoneyUtil.fee2yuan(dictsw).intValue();
            dictchengyu = MoneyUtil.fee2yuan(dictchengyu).intValue();
            cidianhy = MoneyUtil.fee2yuan(cidianhy).intValue();
            gs300 = MoneyUtil.fee2yuan(gs300).intValue();
            dictkxios = MoneyUtil.fee2yuan(dictkxios).intValue();
            dictswios = MoneyUtil.fee2yuan(dictswios).intValue();
            poemios = MoneyUtil.fee2yuan(poemios).intValue();


            int total = wyw + dict + poem + yuwenWeixin + dictkx + dictsw + dictchengyu + cidianhy + gs300 + kkdictios + dictkxios + dictswios + poemios;

            dates.add(date);
            allDatas.add(total);
            dictDatas.add(dict);
            dictiosDatas.add(kkdictios);
            poemDatas.add(poem);
            wywDatas.add(wyw);
            yuwenWeixinDatas.add(yuwenWeixin);
            dictkxDatas.add(dictkx);
            dictswDatas.add(dictsw);
            dictchengyuDatas.add(dictchengyu);
            dicianhyDatas.add(cidianhy);
            gs300Datas.add(gs300);
            dictkxiosDatas.add(dictkxios);
            dictswiosDatas.add(dictswios);
            poemiosDatas.add(poemios);

        }
        mmap.put("dates", dates);
        mmap.put("allDatas", allDatas);
        mmap.put("dictDatas", dictDatas);
        mmap.put("dictiosDatas", dictiosDatas);
        mmap.put("poemDatas", poemDatas);
        mmap.put("wywDatas", wywDatas);
        mmap.put("yuwenWeixinDatas", yuwenWeixinDatas);
        mmap.put("dictkxDatas", dictkxDatas);
        mmap.put("dictswDatas", dictswDatas);
        mmap.put("dictchengyuDatas", dictchengyuDatas);
        mmap.put("cidianhyDatas", dicianhyDatas);
        mmap.put("gs300Datas", gs300Datas);
        mmap.put("dictkxiosDatas", dictkxiosDatas);
        mmap.put("dictswiosDatas", dictswiosDatas);
        mmap.put("poemiosDatas", poemiosDatas);

        return prefix + "/paystat";
    }


    @RequiresPermissions("payment:payrate:view")
    @GetMapping("/paystatmonth")
    public String paystatmonth(ModelMap mmap) {

        List<String> months = new ArrayList<>();
        List<Integer> allDatas = new ArrayList<>();
        List<Integer> dictDatas = new ArrayList<>();
        List<Integer> poemDatas = new ArrayList<>();
        List<Integer> wywDatas = new ArrayList<>();
        List<Integer> yuwenWeixinDatas = new ArrayList<>();
        List<Integer> dictkxDatas = new ArrayList<>();
        List<Integer> dictswDatas = new ArrayList<>();
        List<Integer> dictchengyuDatas = new ArrayList<>();
        List<Integer> dicianhyDatas = new ArrayList<>();
        List<Integer> gs300Datas = new ArrayList<>();
        List<Integer> dictiosDatas = new ArrayList<>();
        List<Integer> dictkxiosDatas = new ArrayList<>();
        List<Integer> dictswiosDatas = new ArrayList<>();
        List<Integer> poemiosDatas = new ArrayList<>();

        Date now = DateUtils.getNowDate();

        for (int i = 12; i >= 0; i--) {

            Date date = DateUtils.addMonths(now, i * -1);

            String month = DateUtils.parseDateToStr(DateUtils.YYYY_MM, date);

            int wyw = orderService.getOrderMonthAmount("wyw", "", month, 1);
            int yingyu = orderService.getOrderMonthAmount("yingyuword", "", month, 1);
            int dict = orderService.getOrderMonthAmount("kkdict", "", month, 1);
            int poem = orderService.getOrderMonthAmount("kkpoem", "", "apple", month, 1);
            int yuwenWeixin = orderService.getOrderMonthAmount("kkyuwen100", "", month, 1);
            int dictkx = orderService.getOrderMonthAmount("dictkx", "", month, 1);
            int dictsw = orderService.getOrderMonthAmount("dictsw", "", month, 1);
            int dictchengyu = orderService.getOrderMonthAmount("dict_chengyu", "", month, 1);
            int cidianhy = orderService.getOrderMonthAmount("cidianhy", "", month, 1);
            int gs300 = orderService.getOrderMonthAmount("gs300", "", month, 1);
            int dictios = orderService.getOrderMonthAmount("kkdictios", "", month, 1);
            int dictkxios = orderService.getOrderMonthAmount("dictkxios", "", month, 1);
            int dictswios = orderService.getOrderMonthAmount("dictswios", "", month, 1);
            int poemios = orderService.getOrderMonthAmount("kkpoem", "apple", month, 1);


            wyw = MoneyUtil.fee2yuan(wyw).intValue();
            yingyu = MoneyUtil.fee2yuan(yingyu).intValue();
            dict = MoneyUtil.fee2yuan(dict).intValue();
            poem = MoneyUtil.fee2yuan(poem).intValue();
            yuwenWeixin = MoneyUtil.fee2yuan(yuwenWeixin).intValue();
            dictkx = MoneyUtil.fee2yuan(dictkx).intValue();
            dictsw = MoneyUtil.fee2yuan(dictsw).intValue();
            dictchengyu = MoneyUtil.fee2yuan(dictchengyu).intValue();
            cidianhy = MoneyUtil.fee2yuan(cidianhy).intValue();
            gs300 = MoneyUtil.fee2yuan(gs300).intValue();
            dictios = MoneyUtil.fee2yuan(dictios).intValue();
            dictkxios = MoneyUtil.fee2yuan(dictkxios).intValue();
            dictswios = MoneyUtil.fee2yuan(dictswios).intValue();
            poemios = MoneyUtil.fee2yuan(poemios).intValue();


            int total = wyw + yingyu + dict + poem + yuwenWeixin + dictkx + dictsw + dictchengyu + cidianhy + gs300 + dictios + dictkxios + dictswios + poemios;

            months.add(month);
            allDatas.add(total);
            dictDatas.add(dict);
            poemDatas.add(poem);
            wywDatas.add(wyw);
            yuwenWeixinDatas.add(yuwenWeixin);
            dictkxDatas.add(dictkx);
            dictswDatas.add(dictsw);
            dictchengyuDatas.add(dictchengyu);
            dicianhyDatas.add(cidianhy);
            gs300Datas.add(gs300);
            dictiosDatas.add(dictios);
            dictkxiosDatas.add(dictkxios);
            dictswiosDatas.add(dictswios);
            poemiosDatas.add(poemios);
        }
        mmap.put("months", months);
        mmap.put("allDatas", allDatas);
        mmap.put("dictDatas", dictDatas);
        mmap.put("poemDatas", poemDatas);
        mmap.put("wywDatas", wywDatas);
        mmap.put("yuwenWeixinDatas", yuwenWeixinDatas);
        mmap.put("dictkxDatas", dictkxDatas);
        mmap.put("dictswDatas", dictswDatas);
        mmap.put("dictchengyuDatas", dictchengyuDatas);
        mmap.put("cidianhyDatas", dicianhyDatas);
        mmap.put("gs300Datas", gs300Datas);
        mmap.put("dictiosDatas", dictiosDatas);
        mmap.put("dictkxiosDatas", dictkxiosDatas);
        mmap.put("dictswiosDatas", dictswiosDatas);
        mmap.put("poemiosDatas", poemiosDatas);

        return prefix + "/paystatmonth";
    }

    @RequiresPermissions("payment:payrate:view")
    @GetMapping("/paymonthstat")
    public String paymonthstat(ModelMap mmap, String month) {

        if (StringUtils.isEmpty(month)) {
            month = DateUtils.getMonth();
        }

        int wyw = orderService.getOrderMonthAmount("wyw", "", month, 1);
        int dict = orderService.getOrderMonthAmount("kkdict", "", month, 1);
        int poem = orderService.getOrderMonthAmount("kkpoem", "", "apple", month, 1);
        int yuwenWeixin = orderService.getOrderMonthAmount("kkyuwen100", "", month, 1);
        int dictkx = orderService.getOrderMonthAmount("dictkx", "", month, 1);
        int dictsw = orderService.getOrderMonthAmount("dictsw", "", month, 1);
        int dictchengyu = orderService.getOrderMonthAmount("dict_chengyu", "", month, 1);
        int cidianhy = orderService.getOrderMonthAmount("cidianhy", "", month, 1);
        int gs300 = orderService.getOrderMonthAmount("gs300", "", month, 1);
        int kkdictios = orderService.getOrderMonthAmount("kkdictios", "", month, 1);
        int dictkxios = orderService.getOrderMonthAmount("dictkxios", "", month, 1);
        int dictswios = orderService.getOrderMonthAmount("dictswios", "", month, 1);
        int poemios = orderService.getOrderMonthAmount("kkpoem", "apple", month, 1);

        wyw = MoneyUtil.fee2yuan(wyw).intValue();
        dict = MoneyUtil.fee2yuan(dict).intValue();
        poem = MoneyUtil.fee2yuan(poem).intValue();
        yuwenWeixin = MoneyUtil.fee2yuan(yuwenWeixin).intValue();
        dictkx = MoneyUtil.fee2yuan(dictkx).intValue();
        dictsw = MoneyUtil.fee2yuan(dictsw).intValue();
        dictchengyu = MoneyUtil.fee2yuan(dictchengyu).intValue();
        cidianhy = MoneyUtil.fee2yuan(cidianhy).intValue();
        gs300 = MoneyUtil.fee2yuan(gs300).intValue();
        kkdictios = MoneyUtil.fee2yuan(kkdictios).intValue();
        dictkxios = MoneyUtil.fee2yuan(dictkxios).intValue();
        dictswios = MoneyUtil.fee2yuan(dictswios).intValue();
        poemios = MoneyUtil.fee2yuan(poemios).intValue();


        mmap.put("month", month);
        mmap.put("wyw", wyw);
        mmap.put("dict", dict);
        mmap.put("poem", poem);
        mmap.put("yuwenWeixin", yuwenWeixin);
        mmap.put("dictkx", dictkx);
        mmap.put("dictsw", dictsw);
        mmap.put("dictchengyu", dictchengyu);
        mmap.put("cidianhy", cidianhy);
        mmap.put("gs300", gs300);
        mmap.put("kkdictios", kkdictios);
        mmap.put("dictkxios", dictkxios);
        mmap.put("dictswios", dictswios);
        mmap.put("poemios", poemios);

        return prefix + "/paymonthstat";
    }

    @RequiresPermissions("payment:payrate:view")
    @GetMapping("/paychannel")
    public String paychannel(ModelMap mmap, Integer days) {

        List<String> months = new ArrayList<>();
        List<Integer> allDatas = new ArrayList<>();
        List<Integer> androidDatas = new ArrayList<>();
        List<Integer> iosDatas = new ArrayList<>();
        List<Integer> harmonyDatas = new ArrayList<>();

        Date now = DateUtils.getNowDate();

        for (int i = days; i >= 0; i--) {

            Date date = DateUtils.addMonths(now, i * -1);

            String month = DateUtils.parseDateToStr(DateUtils.YYYY_MM, date);

            int android = 0;
            int ios = 0;
            int harmony = 0;

            List<Order> orders = orderService.getOrderListMonth(month, 1);
            for (Order order : orders) {
                String channel = order.getChannel();
                if (StringUtils.equalsIgnoreCase(channel, "ios")) {
                    ios += order.getTotalFee();
                } else if (StringUtils.equalsIgnoreCase(channel, "harmony")) {
                    harmony += order.getTotalFee();
                } else {
                    android += order.getTotalFee();
                }
            }

            android = MoneyUtil.fee2yuan(android).intValue();
            ios = MoneyUtil.fee2yuan(ios).intValue();
            harmony = MoneyUtil.fee2yuan(harmony).intValue();

            int total = android + ios + harmony;

            months.add(month);
            allDatas.add(total);
            androidDatas.add(android);
            iosDatas.add(ios);
            harmonyDatas.add(harmony);

        }
        mmap.put("months", months);
        mmap.put("allDatas", allDatas);
        mmap.put("androidDatas", androidDatas);
        mmap.put("iosDatas", iosDatas);
        mmap.put("harmonyDatas", harmonyDatas);
        return prefix + "/paychannel";
    }

}
